import { __spreadArray } from "tslib";
import { ChecklistDefaultProps } from './props';
import mixinValue from '../mixins/value';
import fmtEvent from '../_util/fmtEvent';
Component({
    props: ChecklistDefaultProps,
    mixins: [
        mixinValue({
            transformValue: function (val) {
                var value = val || [];
                return {
                    needUpdate: true,
                    value: value,
                };
            },
        }),
    ],
    methods: {
        onChange: function (item) {
            var _a = this.props, multiple = _a.multiple, options = _a.options, onChange = _a.onChange;
            var value = item.value;
            if (multiple) {
                var currentValue_1 = this.getValue();
                if (currentValue_1.indexOf(value) > -1) {
                    currentValue_1 = currentValue_1.filter(function (v) { return v !== value; });
                }
                else {
                    currentValue_1 = __spreadArray(__spreadArray([], currentValue_1, true), [value], false);
                }
                if (!this.isControlled()) {
                    this.update(currentValue_1);
                }
                if (onChange) {
                    onChange(currentValue_1, options.filter(function (v) { return currentValue_1.indexOf(v.value) > -1; }), fmtEvent(this.props));
                }
            }
            else {
                if (!this.isControlled()) {
                    this.update(value);
                }
                if (onChange) {
                    onChange(value, options.find(function (v) { return v.value === value; }), fmtEvent(this.props));
                }
            }
        },
    },
});
